package com.nari.ies.ne.nw.server.activator;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import com.nari.ies.ne.nw.server.mapper.FileDataCheckMapper;
import com.nari.ies.ne.nw.server.service.fileDataCheck.*;
import com.nari.ies.ne.nw.server.service.fileQuality.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.nari.ies.ne.nw.server.entity.FileQualityEntity;
import com.nari.ies.ne.nw.server.util.DateUtil;
import com.nari.ies.ne.nw.server.util.PropUtil;


@Component
@Slf4j
public class FileCheckActivator {
    @Autowired
    private IFileQualityService fileQualityService;

    @Autowired
    private IFileDataCheckService fileDataCheckService;

    @Autowired
    private FileDataCheckMapper fileDataCheckMapper;

    @Scheduled(cron = "0 0/15 * * * ?")
    //@Scheduled(cron = "0 58 23  * * ?")
    public void TaskDQFileQuality() {
//        System.out.println("-----短期开始时间-----" + new Date());
        log.info("-----短期开始时间-----" + new Date());
        Date nowDate = null;
        try {
            nowDate = DateUtil.getBeforeOrAfterDate(DateUtil.getToday(), -1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //String nowTime = DateUtil.SDF.format(nowDate);
        String localPath = PropUtil.getProperties().getProperty("filePath");
        String successpath = PropUtil.getProperties().getProperty("moveSuccessPath");
        String failPath = PropUtil.getProperties().getProperty("moveFailPath");
        //String equipmentStatus = PropUtil.getProperties().getProperty("status");
        File dirFile = new File(localPath);
        //FileQualityEntity fileQuality;
        FileQualityEntity fileQuality = new FileQualityEntity();
        String fileName = null;

        //主备机参数
 //       int status = Integer.parseInt(equipmentStatus);
//		不判主备机
		int status = 3;

//        System.out.println("短期-主备机状态查询-------------" + status);
 //       log.info("短期-主备机状态查询-------------" + status);
        if (dirFile.exists()) {
            if (dirFile.isDirectory()) {
                File[] files = dirFile.listFiles();
                assert files != null;
                for (File file : files) {
                    if (file.isFile()) {
                        try {
                            fileName = file.getName();
                            if (status == 3) {
//                                System.out.println("准备检测----------------" + fileName);
                                log.info("准备检测----------------" + fileName);
                                String[] fileNameArr = file.getName().split("_");
                                if (5 != fileNameArr.length) {
                                    fileQuality.setFileName(fileName);
                                    fileQuality.setFileType("");
                                    fileQuality.setPlantCode("");
                                    fileQuality.setMessage("文件名称格式有误");
                                    fileQuality.setForecastDate(new Date());
                                    fileQuality.setPlantType("");
                                    fileQuality.setId(null);
                                    fileQuality.setIsFileDelay(0);
                                    fileQuality.setIsFileEmpty(0);
                                    fileQuality.setIsFileMissTag(0);
                                    fileQuality.setIsFileDefect(0);
                                    fileQuality.setIsFileFormatError(0);
                                    fileQuality.setIsFileContentError(0);
                                    fileQualityService.insertFileQualityData(fileQuality);
                                    PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                    continue;
                                }
                                String fileType = fileNameArr[2];
//                              String fileTime = fileNameArr[3];
                                if ("DQYC".equals(fileType)) {
//                                    System.out.println("文件" + fileName + "进入if判断--");
                                    fileQuality = fileQualityService.checkFileName(file);
                                    if (fileQuality.getMessage().equals("success")) {
                                        fileQuality = fileQualityService.DQYCFileQuality(file, fileQuality);

                                        if (!fileQuality.getMessage().equals("success")) {
                                            fileQualityService.insertFileQualityData(fileQuality);
                                            PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                        } else {
                                            if("FD".equals(fileQuality.getPlantType())){
                                                fileDataCheckService.FDDQYCFileDataCheck(file);
                                                PropUtil.moveFile(localPath + File.separator + fileName, successpath + File.separator + fileName);
                                            }else if("GF".equals(fileQuality.getPlantType())){
                                                fileDataCheckService.GFDQYCDataCheck(file);
                                                PropUtil.moveFile(localPath + File.separator + fileName, successpath + File.separator + fileName);
                                            }

                                        }
                                    } else {
                                        PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                    }
                                }
                            } else {
                                PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    @Scheduled(cron = "0 0/15 * * * ?")
    //@Scheduled(cron = "0 20 10 * * ?")
    public void TaskFileQuality() {
//        System.out.println("-----开始时间-----" + new Date());
        log.info("其他预测-----开始时间-----" + new Date());
        String localPath = PropUtil.getProperties().getProperty("filePath");
        String successpath = PropUtil.getProperties().getProperty("moveSuccessPath");
        String failPath = PropUtil.getProperties().getProperty("moveFailPath");
//      String equipmentStatus = PropUtil.getProperties().getProperty("status");

        File dirFile = new File(localPath);

        FileQualityEntity fileQuality;

        String fileName = null;
        //主备机参数
        //  int status = Integer.parseInt(equipmentStatus);

        //不判主备机
		Integer status = 3;
        System.out.println("主备机状态查询结果:---------------------" + status);
        log.info("主备机状态查询结果:---------------------" + status);
        if (dirFile.exists()) {

            if (dirFile.isDirectory()) {
                File[] files = dirFile.listFiles();
                assert files != null;
                for (File file : files) {
                    if (file.isFile()) {
                        try {
                            fileName = file.getName();
                            if (status == 3) {
//                                System.out.println("准备检测----------------" + fileName);
                                log.info("准备检测----------------" + fileName);
                                String[] fileNameArr = file.getName().split("_");
                                String fileType = fileNameArr[2];
                                if (fileType.equals("DQYC")) {
                                    continue;
                                }
                                fileQuality = fileQualityService.checkFileName(file);

                                if ("FD".equals(fileQuality.getPlantType())) {
                                    if (fileQuality.getMessage().equals("success")) {
                                        if ("ZTXX-QXHJ".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.FdQXHJFileQuality(file, fileQuality);
                                        }
                                        if ("CDQYC".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.CDQYCFileQuality(file, fileQuality);
                                        }
                                        if ("FDJZ".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.FDJZFileQuality(file, fileQuality);
                                        }
                                        if ("TJXX".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.FDTJXXFileQuality(file, fileQuality);
                                        }
                                        if ("SYZXX".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.FdSYZXXFileQuality(file, fileQuality);
                                        }
                                        if (!fileQuality.getMessage().equals("success")) {
                                            fileQualityService.insertFileQualityData(fileQuality);
                                            PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                        } else {

                                            if ("ZTXX-QXHJ".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.FDQXHJFileDataCheck(file);
                                            }
                                            if ("CDQYC".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.FDCDQFileDataCheck(file);
                                            }
                                            if ("FDJZ".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.FDJZFileDataCheck(file);
                                            }
                                            if ("TJXX".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.FDTJXXFileDataCheck(file);
                                            }
                                            if ("SYZXX".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.FDSYZXXFileDataCheck(file);
                                            }
                                            PropUtil.moveFile(localPath + File.separator + fileName, successpath + File.separator + fileName);
                                        }
                                    } else {
                                        fileQualityService.insertFileQualityData(fileQuality);
                                        PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                    }
                                }
                                if ("GF".equals(fileQuality.getPlantType())) {
                                    if (fileQuality.getMessage().equals("success")) {
                                        if ("CDQYC".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.CDQYCFileQuality(file, fileQuality);
                                        }
                                        if ("SYZXX".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.GfSYZXXFileQuality(file, fileQuality);
                                        }
                                        if ("TJXX".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.GFTJXXFileQuality(file, fileQuality);
                                        }
                                        if ("ZTXX-QXHJ-TYGZ".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.GfQXHJFileQuality(file, fileQuality);
                                        }
                                        if ("NBQ-HLX".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.GfNBQ_HLXFileQuality(file, fileQuality);
                                        }
                                        if ("XB-FZ".equals(fileQuality.getFileType())) {
                                            fileQuality = fileQualityService.GfXBFZFileQuality(file, fileQuality);
                                        }
                                        if (!fileQuality.getMessage().equals("success")) {
                                            fileQualityService.insertFileQualityData(fileQuality);
                                            PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                        } else {

											if ("CDQYC".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.GFCDQYCDataCheck(file);
											}
											if ("SYZXX".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.GFSYZXXDataCheck(file);
											}
											if ("TJXX".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.GFTJXXDataCheck(file);
											}
											if ("ZTXX-QXHJ-TYGZ".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.GFQXHJDataCheck(file);
											}
											if ("NBQ-HLX".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.GFNBQHLXDataCheck(file);
											}
											if ("XB-FZ".equals(fileQuality.getFileType())) {
                                                fileDataCheckService.GFXBFZDataCheck(file);
											}

                                            PropUtil.moveFile(localPath + File.separator + fileName, successpath + File.separator + fileName);
                                        }
                                    } else {
                                        PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                                    }
                                }
                            } else {
                                PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ArrayIndexOutOfBoundsException e) {
                            System.out.println("捕获到索引越界异常：" + fileName);
                            try {
                                PropUtil.moveFile(localPath + File.separator + fileName, failPath + File.separator + fileName);
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
